<?php
/**
 * User: ly
 * Date: 2017/3/23
 * Time: 09:54
 * Intro:支付宝注册service类
 */

class AlipayRegService extends BaseService
{
	public function VerifyBrowserType(){
		$type = $_SERVER['HTTP_USER_AGENT'];
		$browser = array('Alipay','DingTalk','MicroMessenger','Chrome');
		for($i=0;$i<count($browser);$i++){
			if(strpos($type,$browser[$i])){
				$type = $browser[$i];
				break;
			}
		}
		return $type;
	}

	public function AlipayUserRegister($request){

		$request['userId'] = session('userId');
		$request['alipayId'] = session('alipayId');
		$request['alipayUserId'] = session('alipayUserId');
		$condition['mobile'] = $request['mobile'];
		$condition['real_name'] = $request['real_name'];
		$condition['account_id'] = $request['account_id'];
		$condition['origin'] = 'register';
		$notifyUrl = "/".$request['_URL_'][0]."/".$request['_URL_'][1]."/".$request['_URL_'][2]."/".$request['_URL_'][3]."/".$request['_URL_'][4];
		$UserInfo = self::getUserInfo($condition,$request);
		if($UserInfo){
			//更新滨江后勤用户学校
			if($request['account_id'] == 2){
				M("UserInfo")->where(array('user_id'=>$UserInfo['user_id']))->update(array('school_id'=>$request['school_id']));
			}
			//添加用户信息到gc_alipay_user表
			$saveAlipay = array(
				'alipay_user_id'=>$request['alipayUserId'],
				'alipay_id' => $request['alipayId'],
				'user_id' =>$UserInfo['user_id'],
				'sex'=>$UserInfo['sex'],
				'user_type_value' => 2,
			);
			M("AlipayUser")->update($saveAlipay);
			//更新gc_user表
			$status = 1;
			if($UserInfo['status'] == 2){
				$status = 2;
			}
			$saveGc = array(
				'user_id'=>$UserInfo['user_id'],
				'status' => $status,
				'origin' => 'register',
				'is_binding_alipay' => 1,
				'update_time' =>time()
			);
			M('User')->update($saveGc);
			return array(
				'user_id' =>$UserInfo['user_id'],
				'account_id' =>$request['account_id'],
				'alipayId'=>$request['alipayId'],
				'alipayUserId'=>$request['alipayUserId'],
				'status' => $status,
				'notify_url' =>$notifyUrl,
			);
		}else{
			return false;
		}
	}

	static  function getUserInfo($condition,$request){
		$field = "user_id,account_id,type,real_name,number,status,sex,mobile,origin";
		$userInfo = M('User')->where($condition)->field($field)->limit(1)->find();
		if($userInfo){
			return $userInfo;
		}else{
			$condition['origin'] = 'import';
			$userInfo = M('User')->where($condition)->field($field)->limit(1)->find();
			if($userInfo){
				return $userInfo;
			}else{
				$condition['origin'] = 'binding';
				$userInfo = M('User')->where($condition)->field($field)->limit(1)->find();
				if($userInfo){
					return $userInfo;
				}else{
					if (!session('?regNum')) {
						session('regNum', 1);
						return false;
					} elseif (session('regNum') == 1) {
						$array['account_id'] = $request['account_id'];
						$array['auth_time'] = 0;
						$array['status'] = 1;
						$rest = M('User')->update($array);
						if ($rest) {
							$alSave = array(
								'user_id' => $rest['user_id'],
								'alipay_user_id' => session("alipayUserId")
							);
							$res = M('alipay_user')->update($alSave);
							if($res){
								session('userId', $rest['user_id']);
							}
							$uSer = Service('User');
							$array = array_merge($array, $request);
							$uSer->updateUserInfo($array, $rest['user_id']); //更新userInfo表
							return array(
								'user_id'=>$rest['user_id'],
								'account_id'=>$request['account_id'],
								'type'=>2,
								'real_name'=>$request[''],
								'number'=>$request['mobile'],
								'status'=>1,
								'sex'=>0,
								'mobile'=>$request['real_name'],
								'origin'=>'register'
							);
						}
					}
				}
			}
		}
	}


  
}